<template>
  <div>
    <!-- dialog弹出框 -->
    <el-dialog
      title="批量赋分"
      :visible.sync="show"
      width="50%"
      :before-close="closeDialog"
    >
      <!-- from表单 -->
      <div class="from-infomations">
        <el-form ref="form" label-position="left" :model="sendInfo" label-width="8rem">
          <el-form-item label="当前选中人数">
            {{ userNum }}人
          </el-form-item>
          <el-form-item v-if="routerRules.routerScoreType ===1" label="评分机制">
            加分制
          </el-form-item>
          <el-form-item v-if="routerRules.routerScoreType ===2" label="评分机制">
            扣分制
          </el-form-item>
          <el-form-item v-if="routerRules.routerScoreType ===3" label="选择评分机制">
            <el-select v-model="scoreRules" style="width:13rem" placeholder="请选择" :change="getScoreType()">
              <el-option
                v-for="item in scoreAllRule"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="当前基础分">
            {{ routerRules.routerBaseScore }}分
          </el-form-item>
          <el-form-item label="可赋值最高分">
            {{ routerRules.routerMaxScore }}分
          </el-form-item>
          <el-form-item v-if="routerRules.routerMinScore !== null&&routerRules.routerMinScore !== -99999" label="可赋值最低分">
            {{ routerRules.routerMinScore }}分
          </el-form-item>
          <el-form-item v-if="routerRules.routerMinScore === -99999" label="可赋值最低分">
            该项目无最低分限制
          </el-form-item>
          <!-- 输入分数 -->
          <el-form-item v-if="scoreRules !== ''" label="分数">
            <el-tooltip class="form-score" effect="dark" :content="scoreTips" placement="bottom-start">
              <el-input
                v-model="sendInfo.sendScore"
                style="width:13rem"
                placeholder="请输入分数"
                clearable
                oninput="value=value.replace(/^\.+|[^\d.-]/g,'')"
                maxlength="4"
                show-word-limit
                @input="changeSendScore"
              />
            </el-tooltip>
          </el-form-item>
          <el-form-item v-if="scoreRules === ''" label="分数">
            请选择评分机制，选择后方可输入分数
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog()">取 消</el-button>
        <el-button type="primary" @click="sendDialog()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { batchScores } from '../../api/send.js'
import { getSlist } from '../../api/slist.js'
import { getTlist } from '../../api/tlist.js'
import { isOpenSendTime } from '../../utils/isOpenSendTime.js'
export default {
  props: {
    // 状态获取
    getStatusType: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      // 获取全部数据存储位置
      show: false,
      sendInfo: {
        sendId: '',
        sendTitle: '',
        sendInfo: '',
        sendFlistId: '',
        sendSlistId: '',
        sendTlistId: '',
        sendUserId: '',
        sendStatus: '',
        sendScore: '',
        sendImages: '',
        sendGiveUserId: ''
      },
      userInfo: {
        userId: '',
        userPwd: '',
        userType: '',
        userBranch: '',
        userName: '',
        userReconsider: '',
        userScore: ''
      },
      routerRules: {
        routerId: '',
        routerName: '',
        routerScoreType: '',
        routerBaseScore: '',
        routerMaxScore: '',
        routerMinScore: ''
      },
      allScore: [],
      // 代码数据存储位置
      sendStatus: '',
      scoreRules: '',
      scoreAllRule: [
        {
          value: '1',
          label: '加分制'
        },
        {
          value: '2',
          label: '扣分制'
        }
      ],
      scoreTips: '',
      sendScore: '',
      userNum: '',
      timeType: '' // 时间状态(1.在开放学期内2.还未到开放时间3.已过开放时间)
    }
  },
  created() {
    // dialog信息初始化
    this.createDialogStatus()
  },
  methods: {
    // 展示dialog
    showDialog(data) {
      this.show = true
      var that = this
      //  获取用户信息
      that.statusType = data.allSend
      that.userNum = that.statusType.length
      that.findRouterRules()
    },
    // 获取当前路由分数类型和分数上下限
    findRouterRules() {
      var that = this
      var slist = sessionStorage.getItem('slist')
      var tlist = sessionStorage.getItem('tlist')
      if (tlist === 'nolook') {
        new Promise(function(resolve, reject) {
          getSlist(slist).then(res => {
            resolve(res.obj)
          })
        }).then((data) => {
          that.routerRules.routerId = data.slistId
          that.routerRules.routerName = data.slistName
          that.routerRules.routerScoreType = data.slistScoreType
          that.routerRules.routerBaseScore = data.slistBaseScore
          that.routerRules.routerMaxScore = data.slistMaxScore
          that.routerRules.routerMinScore = data.slistMinScore
          if (data.slistScoreType !== 3) {
            that.scoreRules = data.slistScoreType
            if (data.slistScoreType === 1) {
              that.scoreTips = '当前为加分制，输入1即代表加1分'
            } else if (data.slistScoreType === 2) {
              that.scoreTips = '当前为扣分制，输入1即代表扣1分'
            }
          } else if (data.slistScoreType === 3) {
            that.scoreRules = ''
          }
        })
      } else {
        new Promise(function(resolve, reject) {
          getTlist(tlist).then(res => {
            resolve(res.obj)
          })
        }).then((data) => {
          that.routerRules.routerId = data.tlistId
          that.routerRules.routerName = data.tlistName
          that.routerRules.routerScoreType = data.tlistScoreType
          that.routerRules.routerBaseScore = data.tlistBaseScore
          that.routerRules.routerMaxScore = data.tlistMaxScore
          that.routerRules.routerMinScore = data.tlistMinScore
          if (data.tlistScoreType !== 3) {
            that.scoreRules = data.tlistScoreType
            if (data.tlistScoreType === 1) {
              that.scoreTips = '当前为加分制，输入1即代表加1分'
            } else if (data.tlistScoreType === 2) {
              that.scoreTips = '当前为扣分制，输入1即代表扣1分'
            }
          } else if (data.tlistScoreType === 3) {
            that.scoreRules = ''
          }
        })
      }
    },
    // 保存所有信息准备提交
    allAdd() {
      var that = this
      var flist = sessionStorage.getItem('flist')
      var slist = sessionStorage.getItem('slist')
      var tlist = sessionStorage.getItem('tlist')
      var userId = sessionStorage.getItem('userId')
      var sendSemseterId = sessionStorage.getItem('semId')
      // 封装数据
      for (let i = 0; i < that.statusType.length; i++) {
        let userSendScore = 0
        let sendScore = 0
        if (that.statusType[i].send.sendScore === undefined || that.statusType[i].send.sendScore === null || that.statusType[i].send.sendScore === '') {
          userSendScore = 0
          sendScore = that.sendEndSendScore
        } else {
          userSendScore = parseFloat(that.statusType[i].send.sendScore)
          sendScore = parseFloat(that.sendEndSendScore) + userSendScore
        }
        if (sendScore > that.routerRules.routerMaxScore) {
          sendScore = that.routerRules.routerMaxScore
        }
        if (sendScore < that.routerRules.routerMinScore) {
          sendScore = that.routerRules.routerMinScore
        }
        that.allScore.push({
          userId: that.statusType[i].userId,
          flistId: flist,
          slistId: slist,
          tlistId: tlist,
          sendSemseterId: sendSemseterId,
          userName: userId,
          sendScore: sendScore
        })
      }
    },
    // 上传最终数据
    pushEndScore() {
      var that = this
      new Promise(function(resolve, reject) {
        batchScores(that.allScore).then((res) => {
          resolve(res.obj)
        })
      }).then((data) => {
        that.show = false

        that.routerRules.routerScoreType === ''
        top.location.reload()
      })
    },
    // 页面逻辑方法
    // dialog信息初始化
    createDialogStatus() {
      this.show = false
    },
    // 查看当前分数类型
    getScoreType() {
      var that = this
      var scoreRules = that.scoreRules
      if (scoreRules === '1') {
        that.scoreTips = '当前为加分制，输入1即代表加1分'
      } else if (scoreRules === '2') {
        that.scoreTips = '当前为扣分制，输入1即代表扣1分'
      }
    },
    // 获取当前分数
    changeSendScore(e) {
      var that = this
      that.sendInfo.sendScore = e
    },
    // 关闭dialog
    closeDialog() {
      this.$confirm('是否确定取消', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.show = false
        this.sendInfo.sendScore = ''
        this.$message({
          type: 'success',
          message: '取消评分成功'
        })
      }).catch(() => {})
    },
    // 提交dialog
    sendDialog() {
      var that = this
      var sendInfo = that.sendInfo
      var routerMaxScore = that.routerRules.routerMaxScore
      var routerMinScore = that.routerRules.routerMinScore
      var sendEndScore = that.sendInfo.sendScore
      var scoreRules = that.scoreRules
      var sendEndSendScore = ''
      // 看下是否在开放时间内
      isOpenSendTime().then((res) => {
        that.timeType = res
        if (that.timeType === 1) {
          if (scoreRules === '2' || scoreRules === 2) {
            sendEndSendScore = -sendEndScore
          } else if (scoreRules === '1' || scoreRules === 1) {
            sendEndSendScore = sendEndScore
          }
          var giveSendEndSendScore = parseFloat(sendEndSendScore) + parseFloat(that.routerRules.routerBaseScore)
          that.sendEndSendScore = giveSendEndSendScore
          // 全部逻辑判断
          if (that.scoreRules === '') {
            that.$message({
              message: '请选择评分机制',
              type: 'warning'
            })
          } else if (sendInfo.sendScore === '') {
            that.$message({
              message: '请输入分数',
              type: 'warning'
            })
          } else if (giveSendEndSendScore > routerMaxScore) {
            that.$message({
              message: '请不要大于最高分',
              type: 'warning'
            })
          } else if (giveSendEndSendScore < routerMinScore && routerMinScore !== null) {
            that.$message({
              message: '请不要低于最低分',
              type: 'warning'
            })
          } else if (giveSendEndSendScore < -30) {
            that.$message({
              message: '请不要扣除超过30分',
              type: 'warning'
            })
          } else if (giveSendEndSendScore > 50) {
            that.$message({
              message: '请不要增加超过50分',
              type: 'warning'
            })
          } else {
            // 逻辑判断通过，准备提交
            new Promise(function(resolve, reject) {
              that.$confirm('是否确定提交', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                that.$message('正在提交中')
                resolve(1)
              }).catch(() => {
                resolve(2)
              })
            }).then((data) => {
              if (data === 1) {
                that.allAdd()
                // 批量提交
                if (that.allScore !== null) {
                  that.pushEndScore()
                  that.$message({
                    message: '评分成功',
                    type: 'success'
                  })
                } else {
                  that.$message({
                    message: '当前服务器繁忙，请稍后再试',
                    type: 'warning'
                  })
                }
              } else if (data === 2) {
                this.$message('请继续编辑')
              }
            })
          }
        } else if (that.timeType === 2) {
          that.$message({
            message: '未到开放时间',
            type: 'warning'
          })
        } else if (that.timeType === 3) {
          that.$message({
            message: '已过开放时间',
            type: 'warning'
          })
        } else {
          that.$message({
            message: '系统繁忙请联系管理员',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>
<style lang="scss">

</style>
